<?xml version="1.0"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:atom="http://www.w3.org/2005/Atom"
  xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" 
  xmlns:georss="http://www.georss.org/georss"
  xmlns:aml="http://markup-languages.googlecode.com/ns/aml"
  xmlns:date="http://exslt.org/dates-and-times"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  version="1.0">
    
  <!-- Note: My list of xmlns bindings is not complete here. -->
  <xsl:output method="xml" indent="yes" />
  
  <xsl:template match="/feeds" >
    <aml:aggregated>
      <xsl:for-each select="feed">
        <xsl:element name="aml:blog">
          <xsl:copy-of select="@uri"/>  
          <xsl:apply-templates select="document(@uri)" mode="document">
            <xsl:with-param name='keywords' select='./keyword/@name' />
          </xsl:apply-templates>
        </xsl:element>
      </xsl:for-each>
    </aml:aggregated>
  </xsl:template>
  
  <xsl:template match="rss/channel" mode="document">
	<xsl:param name="keywords"></xsl:param>
    <xsl:for-each select="item">
      <xsl:if test="count($keywords)=0 or ./category/text()=$keywords">
        <xsl:element name="aml:entry">
          <aml:title>
            <xsl:value-of select="title"/>
          </aml:title>
          <aml:author>
            <xsl:choose>
              <xsl:when test="author">
                <xsl:value-of select="author"/>
              </xsl:when>
              <xsl:when test="dc:creator">
                <xsl:value-of select="dc:creator"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:text> Unknown </xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </aml:author>
          <aml:link>
            <xsl:value-of select="link"/>
          </aml:link>
          <aml:pubDate>
            <xsl:value-of select="pubDate"/>
          </aml:pubDate>
          <xsl:apply-templates select="category"/>
          <aml:description>
            <xsl:value-of select="description"/>
          </aml:description>
          <aml:content>
            <xsl:value-of select="content"/>
          </aml:content>
          <xsl:element name="aml:guid">
            <xsl:if test="guid/@isPermaLink">
              <xsl:attribute name="isPermaLink"><xsl:value-of select="guid/@isPermaLink"/></xsl:attribute>
            </xsl:if>
            <xsl:value-of select="guid"/>
          </xsl:element>
        </xsl:element>
      </xsl:if>
     </xsl:for-each>
  </xsl:template>
  
  <xsl:template match="atom:feed" mode="document">
    <xsl:param name="keywords"></xsl:param>
    <xsl:for-each select="atom:entry">   
      <xsl:if test="count($keywords)=0 or ./category/text()=$keywords">
        <aml:entry>
          <aml:title>
            <xsl:value-of select="atom:title"/>
          </aml:title>
          <aml:author>
            <xsl:value-of select="atom:author/atom:name"/>
          </aml:author>
          <aml:link>
            <xsl:value-of select="atom:link/@href"/>
          </aml:link>
          <aml:pubDate>
            <xsl:call-template name="rfc822Date">
              <xsl:with-param name="isoDate" select="atom:published"/>
            </xsl:call-template>
          </aml:pubDate>
          <xsl:apply-templates select="atom:category"/>
          <aml:description>
            <xsl:value-of select="atom:content"/>
          </aml:description>
          <xsl:element name="aml:guid">
            <xsl:attribute name="isPermaLink">false</xsl:attribute>
            <xsl:value-of select="atom:id"/>
          </xsl:element>
        </aml:entry>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
  <xsl:template match="category">
    <xsl:element name="aml:category">
      <xsl:value-of select="."/>
    </xsl:element>
  </xsl:template>
  
  
  <xsl:template match="atom:category">
    <xsl:element name="aml:category">
      <xsl:value-of select="./@term"/>
    </xsl:element>
  </xsl:template>
  
  <xsl:template name="rfc822Date">
    <xsl:param name="isoDate"/>
    <xsl:value-of select="date:day-abbreviation($isoDate)"/>, <xsl:value-of select="format-number(date:day-in-month($isoDate), '00')"/><xsl:text> </xsl:text>
    <xsl:value-of select="date:month-abbreviation($isoDate)"/><xsl:text> </xsl:text>
    <xsl:value-of select="date:year($isoDate)"/><xsl:text> </xsl:text>
    <xsl:value-of select="format-number(date:hour-in-day($isoDate), '00')"/>:<xsl:value-of select="format-number(date:minute-in-hour($isoDate), '00')"/>:<xsl:value-of select="format-number(date:second-in-minute($isoDate), '00')"/> GMT
  </xsl:template>
  
  <xsl:template match='*'/>
</xsl:stylesheet>
